/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diet.dao;

import diet.dto.FoodDTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author DungTT
 */
public class FoodDAO {

    public boolean add(FoodDTO food) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("INSERT INTO [Food](FoodID,CategoryID,FoodName,ImageURL,Calorie,Carb,Protein,Fat,Amount) VALUES(?,?,?,?,?,?,?,?,?)");
            stm.setInt(1, food.getFoodID());
            stm.setInt(2, food.getCategoryID());
            stm.setString(3, food.getFoodName());
            stm.setString(4, food.getImageURL());
            stm.setInt(5, food.getCalorie());
            stm.setFloat(6, food.getCarb());
            stm.setFloat(7, food.getProtein());
            stm.setFloat(8, food.getFat());
            stm.setString(9, food.getAmount());
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }

    public boolean delete(int FoodID) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("DELETE FROM [Food] WHERE FoodID=? ");
            stm.setInt(1, FoodID);
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }

    public boolean update(FoodDTO food) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("UPDATE [Food] SET CategoryID=?, FoodName=?,ImageURL=?,Calorie=?,Carb=?,Protein=?,Fat=?, Amount=? "
                    + " WHERE CategoryID=?");
            stm.setInt(1, food.getCategoryID());
            stm.setString(2, food.getFoodName());
            stm.setString(3, food.getImageURL());
            stm.setInt(4, food.getCalorie());
            stm.setFloat(5, food.getCarb());
            stm.setFloat(6, food.getProtein());
            stm.setFloat(7, food.getFat());
            stm.setString(8, food.getAmount());
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }
}
